<script setup>
import {
  Management,
  Promotion,
  UserFilled,
  User,
  Crop,
  EditPen,
  SwitchButton,
  CaretBottom,
  ChatDotRound
} from '@element-plus/icons-vue'
import avatar from '@/assets/default.png'
import { onMounted } from 'vue'
import {ElMessageBox, ElMessage} from "element-plus";
import {getUserInfoService} from "@/api/user.js";
import {useUserInfoStore} from "@/stores/userInfo.js";
import {useTokenStore} from "@/stores/token.js";
import {useRouter} from "vue-router";

//获取用户信息
const userInfo = useUserInfoStore();
//获取token
const userToken = useTokenStore();
//获取路由
const router = useRouter();
async function getUserInfo(){
  let res = await getUserInfoService()
  userInfo.setUserInfo(res.data)
}
getUserInfo();

//处理下拉菜单点击事件
function handleCommand(command) {
  if (command === "logout") {
    ElMessageBox.confirm("确认退出登录吗？", "温馨提示", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    }).then(() => {
      // 退出登录
      // 清除用户信息
      userInfo.removeUserInfo()
      userToken.removeToken()
      // 跳转到登录页面
      router.push("/login")
      ElMessage.success("退出登录成功")
    }).catch(() => {
      ElMessage.info("取消退出登录")
    });
  } else {
    router.push(command);
  }
}
</script>

<template>
  <!--
    el-menu 整个菜单组件
      :default-active="$route.path"  配置默认高亮的菜单项
      router  router选项开启，el-menu-item 的 index 就是点击跳转的路径

    el-menu-item 菜单项
      index="/article/channel" 配置的是访问的跳转路径，配合default-active的值，实现高亮
  -->
  <el-container class="layout-container">
<!--    左侧菜单栏-->
    <el-aside width="200px">
      <div class="el-aside__logo"></div>
      <el-menu
          active-text-color="#ffd04b"
          background-color="#232323"
          text-color="#fff"
          router
      >
<!--        这里的index表示的是跳转到那个页面，要与router定义的路径一致-->
        <el-menu-item index="/article/category">
          <el-icon><Management /></el-icon>
          <span>文章分类</span>
        </el-menu-item>
        <el-menu-item index="/article/manage">
          <el-icon><Promotion /></el-icon>
          <span>文章管理</span>
        </el-menu-item>

        <el-sub-menu>
          <!-- 多级菜单的标题 - 具名插槽 title -->
          <template #title>
            <el-icon><UserFilled /></el-icon>
            <span>个人中心</span>
          </template>

          <!-- 展开的内容 - 默认插槽 -->
          <el-menu-item index="/user/info">
            <el-icon><User /></el-icon>
            <span>基本资料</span>
          </el-menu-item>
          <el-menu-item index="/user/avatar">
            <el-icon><Crop /></el-icon>
            <span>更换头像</span>
          </el-menu-item>
          <el-menu-item index="/user/password">
            <el-icon><EditPen /></el-icon>
            <span>重置密码</span>
          </el-menu-item>
        </el-sub-menu>

        <el-menu-item index="/websocket">
          <el-icon><ChatDotRound /></el-icon>
          <span>websocket聊天</span>
        </el-menu-item>

        <el-menu-item index="/seckill">
          <el-icon><ChatDotRound /></el-icon>
          <span>秒杀功能demo</span>
        </el-menu-item>
      </el-menu>
    </el-aside>
<!--    右侧主区域-->
    <el-container>
      <el-header>
        <div>尊敬的用户:
          <strong>{{userInfo.userInfo.nickname}}</strong>
        </div>
<!--        下拉菜单-->
        <!-- command: 条目被点击后会触发,在事件函数上可以声明一个参数,接收条目对应的指令 -->
        <el-dropdown placement="bottom-end" @command="handleCommand">
          <!-- 展示给用户，默认看到的 -->
          <span class="el-dropdown__box">
<!--            头像-->
            <el-avatar :src="userInfo.userInfo.userPic ? userInfo.userInfo.userPic : avatar"></el-avatar>
            <el-icon><CaretBottom /></el-icon>
          </span>

          <!-- 折叠的下拉部分 -->
          <!-- 折叠的下拉部分 -->
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item command="/user/info" :icon="User"
              >基本资料</el-dropdown-item
              >
              <el-dropdown-item command="/user/avatar" :icon="Crop"
              >更换头像</el-dropdown-item
              >
              <el-dropdown-item command="/user/password" :icon="EditPen"
              >重置密码</el-dropdown-item
              >
              <el-dropdown-item command="logout" :icon="SwitchButton"
              >退出登录</el-dropdown-item
              >
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </el-header>
      <el-main>
        <RouterView/>
      </el-main>
      <el-footer>大事件 ©2023 Created by 黑马程序员</el-footer>
    </el-container>
  </el-container>
</template>

<style lang="scss" scoped>
.layout-container {
  height: 100vh;
  .el-aside {
    background-color: #232323;
    &__logo {
      height: 120px;
      background: url('@/assets/logo.png') no-repeat center / 120px auto;
    }
    .el-menu {
      border-right: none;
    }
  }
  .el-header {
    background-color: #fff;
    display: flex;
    align-items: center;
    justify-content: space-between;
    .el-dropdown__box {
      display: flex;
      align-items: center;
      .el-icon {
        color: #999;
        margin-left: 10px;
      }

      &:active,
      &:focus {
        outline: none;
      }
    }
  }
  .el-footer {
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 14px;
    color: #666;
  }
}
</style>
